package pers.feng1240.blog.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import pers.feng1240.blog.dao.BlogDao;
import pers.feng1240.blog.dao.TagDao;
import pers.feng1240.blog.dao.ThemeDao;
import pers.feng1240.blog.service.ForegroundService;
import pers.feng1240.blog.util.PageHelper;

@Service("foregroundService")
public class ForegroundServiceImpl implements ForegroundService {

    @Resource
    private BlogDao blogDao;
    @Resource
    private TagDao tagDao;
    @Resource
    private ThemeDao themeDao;
    /*
     * 分页列表
     * @param pageIndex 要获取的页码
     * @see pers.feng1240.blog.service.AbstractService#getPageList(java.lang.String)
     */
    public Map<String, Object> getPageList(int pageIndex) {
        Map<String,Object> mapRes = new HashMap<String,Object>();
        int count = blogDao.objectCount(); //对象总个数
        mapRes.put("count", blogDao.objectCount());
        mapRes.put("pageSize", PageHelper.forePageSize(count));//页数
        mapRes.put("pageIndex", pageIndex); //
        mapRes.put("objectList", blogDao.getPageList(PageHelper.selObjFore(pageIndex)));
        System.out.println(blogDao.getPageList(PageHelper.selObjFore(pageIndex)).size());
        return mapRes;

    }
    
    /*
     *访问量加一
     * @see pers.feng1240.blog.service.ForegroundService#addTimes(int)
     */
	@Override
	public void addTimes(Map<String, Object> map) {
		// TODO Auto-generated method stub
		blogDao.addTimes(map);
	}

	/*
	 * 浏览排行
	 * @see pers.feng1240.blog.service.ForegroundService#timesTop()
	 */
	public List<Object> timesTop() {
		// TODO Auto-generated method stub
		return blogDao.timesTop();
	}

    /*
     * 标签集合
     * @see pers.feng1240.blog.service.ForegroundService#tags()
     */
    public List<Object> tags() {
        // TODO Auto-generated method stub
        return tagDao.getObjectList();
    }

    /*
     * 根据标签获取博客列表
     * @see pers.feng1240.blog.service.ForegroundService#getPageListByTag(int, int)
     */
    public Map<String, Object> getPageListByTag(int pageIndex, int id) {
        Map<String,Object> mapRes = new HashMap<String,Object>();
        
        //1.获取对象个数
        int count = blogDao.getBlogCountByTag(id); //对象总个数
        System.out.println(count);
        //2.
        mapRes.put("count", count);
        mapRes.put("pageSize", PageHelper.forePageSize(count));//页数
        mapRes.put("pageIndex", pageIndex); //
        mapRes.put("objectList", blogDao.getBlogListByTag(PageHelper.selObjTag(pageIndex,id)));
        System.out.println(mapRes);
        return mapRes;
    }

	/*
	 * 主题列表
	 * @see pers.feng1240.blog.service.ForegroundService#theme()
	 */
	public Object theme() {		
		return themeDao.getObjectList();
	}

	/*
	 * 由主题获取博客列表
	 * @see pers.feng1240.blog.service.ForegroundService#getPageListByTheme(int, int)
	 */
	public Object getPageListByTheme(int pageIndex, int id) {
		Map<String,Object> mapRes = new HashMap<String,Object>();    
        //1.获取对象个数
        int count = blogDao.getBlogCountByTheme(id); //对象总个数
        System.out.println(count);
        //2.
        mapRes.put("count", count);
        mapRes.put("pageSize", PageHelper.forePageSize(count));//页数
        mapRes.put("pageIndex", pageIndex); //
        mapRes.put("objectList", blogDao.getBlogListByTheme(PageHelper.selObjTag(pageIndex,id)));
        System.out.println(mapRes);
        return mapRes;
	}

}
